#ifndef _TORRENT_H_
#define _TORRENT_H_

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <math.h>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "sha1.h"
#include "bencode.h"
#include "net.h"
#include "peer.h"
#include "tracker.h"
#include "storage.h"

using namespace std;

class Tracker;
class Peer;

class Torrent
{
public:
	// Constructors
	Torrent(const char *filename);
	~Torrent();

	// Torrent Data
	string GetFileName();
	unsigned GetFileSize();
	unsigned GetPieceSize(unsigned piece_num=0);
	unsigned GetNumPieces();
	unsigned GetUploaded();
	unsigned GetDownloaded();
	unsigned GetIndexOfNextPiece();
	string GetInfoHash();

	// Peer/Tracker Data
	unsigned GetNumPeers();
	unsigned GetNumTrackers();
	Peer* GetPeer(unsigned index);
	Tracker* GetTracker(unsigned index);

private:
	Storage *m_storage;

	string m_filename;
	unsigned m_filesize;
	unsigned m_piecesize;
	unsigned m_numpieces;
	unsigned m_uploaded;
	unsigned m_downloaded;

	unsigned char m_infohash[20];
	unsigned char *m_pieces;
	unsigned char *m_bitfield;

	vector<Peer*> m_peers;
	vector<Tracker*> m_trackers;

	friend class Peer;
	friend class Tracker;
	void DoResume();
	unsigned char* GetRawInfoHash();
	unsigned char* GetPieceHash(unsigned index);
	bool SetHavePiece(unsigned index);
	Storage* GetStorage();
	void AddPeer(Peer *peer);
};

#endif
